package client.ui.view.panel.good;

import client.control.Controller;
import client.control.ResponseEntity;
import client.entity.Comment;
import client.entity.Good;
import client.entity.User;
import client.ui.component.divider.Divider;
import client.ui.component.layout.VFlowLayout;
import client.ui.component.scroll.SmallMouseWheelListener;
import client.ui.theme.ThisFont;
import client.ui.util.MaterialColors;
import client.ui.view.MainPage;
import client.ui.view.base.FrameBase;
import client.ui.view.base.PanelControl;
import client.ui.view.module.CommentItem;

import client.ui.view.panel.main.ChatPanel;
import com.alibaba.fastjson.JSONObject;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
  * @Description 评论框架
  * @author 孔明浩
  * @Date 2020/3/31
  */
public class CommentPanel extends JPanel implements PanelControl{
    private Executor singleThreadExecutor = Executors.newSingleThreadExecutor();
    private Good good;
    private JPanel commentPanel;
    private List<Comment> comments = new ArrayList<>();
    private static final Map<Integer, String> names = new HashMap<>();
    public CommentPanel(Good good){
        this.good = good;
        initial();
    }

    private void initial(){
        this.setLayout(new BorderLayout());
        this.setBackground(MaterialColors.WHITE);
        commentPanel = new JPanel(new VFlowLayout());
        commentPanel.setBackground(MaterialColors.WHITE);
        JScrollPane scrollPane = new JScrollPane(commentPanel);
        scrollPane.setBackground(MaterialColors.WHITE);
        scrollPane.setBorder(BorderFactory.createTitledBorder("评论"));
        scrollPane.addMouseWheelListener(new SmallMouseWheelListener(10,scrollPane.getVerticalScrollBar()));
        this.add(scrollPane, BorderLayout.CENTER);
        //添加分割线
        this.add(new Divider(MaterialColors.COSMO_BLUE), BorderLayout.SOUTH);
    }

    @Override
    public void updatePanel(Object object) {
        if (object instanceof Good){
            Good good = (Good) object;
            ResponseEntity responseEntity = Controller.COMMENT_CONTROL.getCommentByGoodId(good.getGoodId());
            if (responseEntity.getCode() == 0){
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        comments = JSONObject.parseArray(responseEntity.getData(), Comment.class);
                        commentPanel.removeAll();
                        for (Comment c:comments){
                            CommentItem commentItem = new CommentItem(c);
                            commentPanel.add(commentItem);
                            commentItem.addMouseListener(new MouseAdapter() {
                                @Override
                                public void mouseClicked(MouseEvent e) {
                                    singleThreadExecutor.execute(new Runnable() {
                                        @Override
                                        public void run() {
                                            String username = getUsername(c.getUserId());
                                            PanelControl p = PanelControl.panels.get("ChatPanel");
                                            JFrame f = FrameBase.frameBaseList.get(0);
                                            if (p instanceof ChatPanel){
                                                ((ChatPanel) p).selectItem(c.getUserId(), username);
                                            }
                                            if (f instanceof MainPage){
                                                f.setVisible(true);
                                                ((MainPage) f).actionPerformed(
                                                        new ActionEvent(commentItem, ActionEvent.ACTION_PERFORMED, "chatList"));
                                            }

                                        }
                                    });

                                }
                            });
                        }
                        commentPanel.validate();
                        //防止花屏
                        commentPanel.repaint();
                    }
                });

            } else {
                if (commentPanel.getComponents().length == 0){
                    JLabel label = new JLabel("还没有评论哦,发表评论占个沙发吧", SwingConstants.CENTER);
                    label.setFont(ThisFont.BIG_FONT);
                    commentPanel.add(label);
                }
            }

        }
    }

    private String getUsername(int userId){
        if (names.containsKey(userId)){
            return names.get(userId);
        } else {
            ResponseEntity responseEntity = Controller.USER_CONTROL.getOtherUserInfo(userId);
            if (responseEntity.getCode() == 0){
                User user = JSONObject.parseObject(responseEntity.getData(), User.class);
                names.put(userId, user.getUserName());
                return user.getUserName();
            } else {
                return "";
            }
        }
    }
}
